package com.andy.qhb.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.andy.qhb.base.RequestUtil;
import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.UsrPayLog;
import com.andy.qhb.game.GameConf;
import com.andy.qhb.game.GameUtils;
import com.andy.qhb.heepay.SmallTools;
import com.andy.qhb.utils.JsonUtils;

/**
 * Created by Administrator on 2017/9/8.
 */
@Controller
@RequestMapping(value="game/api")
public class HeepayNotifyController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@RequestMapping(value="returnResult")
	public String returnResult() {
		
		return "error";
	}

	@RequestMapping(value="heepayNotify")
    public String heepayNotify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
        //接收异步通知参数
        String result = request.getParameter("result");//支付结果，1=成功（没有其他情况，因为之后支付成功才会下发异步通知）
        String agent_id = request.getParameter("agent_id");//商户ID
        String jnet_bill_no = request.getParameter("jnet_bill_no");//汇付宝订单号
        String agent_bill_id = request.getParameter("agent_bill_id");//商户系统订单号
        String pay_type = request.getParameter("pay_type");//支付类型
        String pay_amt = request.getParameter("pay_amt");//实际支付金额
        String remark = request.getParameter("remark");//商户自定义参数，透传参数
        String sign = request.getParameter("sign");//签名
        
        String respJson = JsonUtils.object2JsonString(RequestUtil.getRequestMap(request));
        logger.error("回调信息：{}",respJson);

        
        //验签
        String b_sign = "result="+result+
                "&agent_id="+agent_id+
                "&jnet_bill_no="+jnet_bill_no+
                "&agent_bill_id="+agent_bill_id+
                "&pay_type="+pay_type+
                "&pay_amt="+pay_amt+
                "&remark="+remark+
                "&key="+GameConf.heepay_key;
        b_sign = SmallTools.MD5en(b_sign);
        if(b_sign.equals(sign)){
            PrintWriter out = response.getWriter();
            out.print("ok");
            
            doSuccess(agent_bill_id,pay_amt,respJson);
        }else {
            PrintWriter out = response.getWriter();
            out.print("error");
        }
        return null;
    }

	private void doSuccess(String agent_bill_id, String pay_amt, String respJson) {
		UsrPayLog payLog = DaoUtils.getOne(UsrPayLog.class, Where.eq("bill_id", agent_bill_id));
		if(payLog != null) {
			payLog.setStatus(1);
			payLog.setRespJson(respJson);
			DaoUtils.update(payLog);
			
			GameUtils.addCoin(payLog.getUid(), (int) (Double.parseDouble(pay_amt)*100));
		}
		
	}
}
